package com.gentlemancredit.app.component.keyboard;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.EditText;

import com.gentlemancredit.app.R;


/**
 * Created by Administrator on 2017/6/3.
 * 自定义密码输入框
 */

@SuppressLint("AppCompatCustomView")
public class PasswordEditText extends EditText {
    //画笔
    private Paint mPaint;
    //一个密码所占的宽度
    private int mPasswordItemWidth;
    //密码的个数默认6个
    private int mPasswordNumder = 6;
    //背景边框颜色
//    private int mBgColor= Color.parseColor("#d1d2d6");
    private int mBgColor = Color.parseColor("#F7F7F7"); //键盘背景颜色
    //背景边框的大小
    private int mBgSize = 1;
    //背景边框圆角大小
    private int mBgCorner = 0;
    //分割线的颜色
    private int mDivisionLineColor = mBgColor;
    //分割线的大小
    private int mDivisionLineSize = 1;
    //密码圆点的颜色
    private int mPasswordColor = mDivisionLineColor;
    //密码圆点的半径大小
    private int mPasswordRadius = 4;

    public PasswordEditText(Context context) {
        this(context, null);
    }

    public PasswordEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        initAttributeSet(context, attrs);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.e("TAG", "onDraw(PasswordEditText.java:61)" + "onDrow。。。。");
        //一个密码的宽度
        mPasswordItemWidth = (getWidth() - 2 * mBgSize - (mPasswordNumder - 1) * mDivisionLineSize) / mPasswordNumder;
        drawBg(canvas);
        drawDivisionLine(canvas);
        drawPassword(canvas);
        //当前密码是不是满了
        if (mListener != null) {
            String password = getText().toString().trim();
            if (password.length() >= mPasswordNumder) {
                Log.e("TAG", "onDraw(PasswordEditText.java:70)" + "");
                mListener.passwordFull(password);
            }
        }
    }

    /**
     * 绘制密码
     *
     * @param canvas
     */
    private void drawPassword(Canvas canvas) {
        //设置实心样式
        mPaint.setTextSize(68f);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mPasswordColor);
        String text = getText().toString().trim();
        int passwordLength = text.length();

        for (int i = 0; i < passwordLength; i++) {
//            int cx = mBgSize + i * mPasswordItemWidth + i * mDivisionLineSize + mPasswordItemWidth / 2;
            int cx = i * mDivisionLineSize + i * mPasswordItemWidth + mPasswordItemWidth / 2 + mBgSize - 20;
            int cy = getHeight() / 2 + 20;
//            canvas.drawCircle(cx, cy, mPasswordRadius, mPaint);

            canvas.drawText(text.charAt(i) + "", Float.valueOf(cx + ""), Float.valueOf(cy + ""), mPaint);
        }

    }

    /**
     * 绘制分割线
     *
     * @param canvas
     */
    private void drawDivisionLine(Canvas canvas) {
        mPaint.setStrokeWidth(mDivisionLineSize);
        mPaint.setColor(mDivisionLineColor);
        for (int i = 0; i < mPasswordNumder - 1; i++) {
            int startX = mBgSize + (i + 1) * mPasswordItemWidth + i * mDivisionLineSize;
            int startY = mBgSize;
            int endX = startX;
            int endY = getHeight() - mBgSize;
            canvas.drawLine(startX, startY, endX, endY, mPaint);

        }
    }

    /**
     * 绘制背景
     *
     * @param canvas
     */
    private void drawBg(Canvas canvas) {
        RectF rect = new RectF(mBgSize, mBgSize, getWidth() - mBgSize, getHeight() - mBgSize);
        //绘制背景  如果有圆角就绘制圆角矩形，没有就绘制矩形
        //设置画笔的大小
        mPaint.setStrokeWidth(mBgSize);
        mPaint.setColor(mBgColor);
        //绘制空心
        mPaint.setStyle(Paint.Style.STROKE);
        if (mBgCorner == 0) {
            canvas.drawRect(rect, mPaint);
        } else {
            canvas.drawRoundRect(rect, mBgCorner, mBgCorner, mPaint);
        }
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
    }

    /**
     * 初始化属性
     *
     * @param context
     * @param attrs
     */
    private void initAttributeSet(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PasswordEditText);
        //获取大小
        mDivisionLineSize = (int) array.getDimension(R.styleable.PasswordEditText_divisionLineSize, dip2px(mDivisionLineSize));
        mPasswordRadius = (int) array.getDimension(R.styleable.PasswordEditText_passwordRadius, dip2px(mPasswordRadius));
        mBgSize = (int) array.getDimension(R.styleable.PasswordEditText_bgSize, dip2px(mBgSize));
        mBgCorner = (int) array.getDimension(R.styleable.PasswordEditText_bgCorner, dip2px(mBgCorner));
        //获取颜色
        mBgColor = array.getColor(R.styleable.PasswordEditText_bgColor, mBgColor);
        mDivisionLineColor = array.getColor(R.styleable.PasswordEditText_divisionLineColor, mDivisionLineColor);
        mPasswordColor = array.getColor(R.styleable.PasswordEditText_passwordColor, mDivisionLineColor);
        array.recycle();
    }

    private float dip2px(int mDivisionLineSize) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mDivisionLineSize, getResources().getDisplayMetrics());
    }

    /**
     * 设置密码
     *
     * @param numder
     */
    public void addPassword(String numder) {
        //把之前的密码取出来
        String password = getText().toString().trim();
        //判断密码的长度，不能超过当前设置的密码的个数
        if (password.length() >= mPasswordNumder) {
//            mListener.passwordFull(password);
            return;
        }
        //密码叠加
        password += numder;
        setText(password);
    }

    /**
     * 删除最后一位密码
     */
    public void deleteLastPassword() {
        //把之前的密码取出来
        String password = getText().toString().trim();
        //判断当前密码是否为空
        if (TextUtils.isEmpty(password)) {
            return;
        }
        password = password.substring(0, password.length() - 1);
        setText(password);
    }

    //清空密码
    public void clearPasswword() {
        setText("");
    }

    //设置接口回调
    private PasswordFullListener mListener;

    public void setOnPasswordFullListener(PasswordFullListener listener) {
        this.mListener = listener;
    }

    public interface PasswordFullListener {
        public void passwordFull(String password);
    }
}
